<?php
namespace app\api\controller;
use think\Request;
use think\Db;
use think\Controller;
use think\Session;
use changpay\ChangPay;
use app\common\controller\order\Orderget;
class Index extends Controller
{
//	账单通知url
     public function mx_bill(){
     	$data = $this->request->param(true);
        if(!empty($data['task_id'])){
			 header("HTTP/1.1 201 Created");
		}
		exit();
       }
    //用户资信报告通知URL   获取 原始数据  用户报告  资信报告  mistr
//  {"mobile":"15239626761","user_id":"1","task_id":"f3172a70-5c0e-11e9-b72a-00163e10becc","bills":["2019-04","2019-03","2019-02","2019-01","2018-12","2018-11"]}
    public function mx_report(){
        $data = $this->request->param(true);
        $phone =$data['mobile'];
        $userId =$data['user_id'];
        $taskId =$data['task_id'];
            $size = ob_get_length();
            header("Content-Length: $size");
            header('Connection: close');
            header("HTTP/1.1 201 OK");
            header("Content-Type: application/json;charset=utf-8");
            ob_end_flush();
            if(ob_get_length())
                ob_flush();
            flush();
            if (function_exists("fastcgi_finish_request")) { // yii或yaf默认不会立即输出，加上此句即可（前提是用的fpm）
                fastcgi_finish_request(); // 响应完成, 立即返回到前端,关闭连接
            }
            ignore_user_abort(true);//在关闭连接后，继续运行php脚本
            set_time_limit(0); //no time limit，不设置超时时间（根据实际情况使用）
            //继续运行的代码
            sleep(10);
		Db::startTrans();
		try {
        $Orderget=new Orderget();
        $token=$Orderget->get_token_moxie();//获取商户号
        $url ='https://api.51datakey.com/carrier/v3/mobiles/'.$phone.'/mxdata-ex?task_id='.$taskId;//原始数据
        $origin_data = httpGet_header_moxie($url,$token);
        $origin = Db::table('xb_moxie_origin')->where('UserID',$userId)->find();
        if($origin){
        	$aaa['OriginData']=$origin_data;
			$calls=json_encode(json_decode($origin_data,JSON_UNESCAPED_UNICODE)['calls'],JSON_UNESCAPED_UNICODE);
//			p($calls);
			$aaa['calls']=$calls;
            Db::table('xb_moxie_origin')->where('UserID',$userId)->update($aaa); 
        }else{
        	$aaa['CreateTime']=date('Y-m-d H:i:s');
			$aaa['OriginData']=$origin_data;
			$aaa['UserID']=$userId;
			$calls=json_encode(json_decode($origin_data,JSON_UNESCAPED_UNICODE)['calls'],JSON_UNESCAPED_UNICODE);
			$aaa['calls']=$calls;
            Db::table('xb_moxie_origin')->insertGetId($aaa); 
        }
        $api_url ="https://api.51datakey.com/carrier/v3/mobiles/{$phone}/mxreport?task_id={$taskId}";//用户报告
        $report_data = httpGet_header_moxie($api_url,$token);
		$origin = Db::table('xb_moxie_report')->where('UserID',$userId)->find();
        if($origin){
        	$bbb['ReportData']=$report_data;
			$TongoHuaPaiHang=json_encode(json_decode($report_data,JSON_UNESCAPED_UNICODE)['call_contact_detail'],JSON_UNESCAPED_UNICODE);
//			p($TongoHuaPaiHang);die;
			$bbb['TongoHuaPaiHang']=$TongoHuaPaiHang;
            Db::table('xb_moxie_report')->where('UserID',$userId)->update($bbb); 
        }else{
        	$bbb['CreateTime']=date('Y-m-d H:i:s');
			$bbb['ReportData']=$report_data;
			$bbb['UserID']=$userId;
			$TongoHuaPaiHang=json_encode(json_decode($report_data,JSON_UNESCAPED_UNICODE)['call_contact_detail'],JSON_UNESCAPED_UNICODE);
		
			$bbb['TongoHuaPaiHang']=$TongoHuaPaiHang;
            Db::table('xb_moxie_report')->insertGetId($bbb); 
        }
		$memUpdate['TrueName2']=$data['name'];
        $memUpdate['isMobile']=1;
        Db::table('xb_mem_info')->where('ID',$userId)->update($memUpdate);//修改用户表里面的手机认证
        $ixExist = Db::table('xb_renzen_mobile')->where('UserID',$data['user_id'])->find();
	    $data_s['ZUserName'] =$data['name'];
        $data_s['mistr']=$data['message'];
        $data_s['mobile']=$data['mobile'];
        if($ixExist){
              $data_s['Status'] ='1';
              $data_s['UpdateTime'] =date('Y-m-d H:i:s');
              Db::table('xb_renzen_mobile')->where('UserID',$data['user_id'])->update($data_s); //跟新手机认证表 真实姓名  加密串  手机号码
        }else{
        	  $data_s['Status'] ='1';
        	  $data_s['RenzTime'] =date('Y-m-d H:i:s');
			  $data_s['TaskID'] =$data['task_id'];
        	  $data_s['UserID'] =$data['user_id'];
              Db::table('xb_renzen_mobile')->insert($data_s); 
        }
		Db::commit();
//		header("HTTP/1.1 201 Created");
		die();
		} catch (\Exception $e) {
			Db::rollback();
			die;
//			header("HTTP/1.1 520 Created");
			
//			p($e->getMessage());die;
		}
    }
    //到期更改禁止借款的订单状态 Linux cortab
//  public function changeOrderStauts(){
//      $time =time();
//      $data = Db::table('xb_loans_applylist')->field('ID,FkTime,ForbidDay')->where('LoanStatus',5)->select();
//      foreach($data as $val){
//          //如果拒绝时间加上被禁天数 等于今天00:00:00 修改状态  4
//          $timetamp1=strtotime($val['FkTime']);
//          $timetamp2=$val['ForbidDay']*86400;
//          $times=$timetamp1+$timetamp2;
//          if(abs($times-$time)<10){
//              Db::table('xb_loans_applylist')->where("ID",$val['ID'])->update(array('LoanStatus'=>4));
//          }
//      }
//  }
}
